package ace.module.product.core.impl.service.helper;

import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.model.SaveContext;
import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.model.SaveListContext;
import ace.cmp.util.AceAssertUtils;
import ace.module.product.api.model.input.PmsProductSkuSaveInput;
import ace.module.product.core.impl.dao.entity.PmsCommonGallery;
import ace.module.product.core.impl.dao.entity.PmsProduct;
import ace.module.product.core.impl.dao.entity.PmsProductSku;
import ace.module.product.core.impl.dao.entity.PmsProductSkuAttributeValue;
import ace.module.product.core.impl.dao.model.bo.PmsProductAttributeAndValueSaveBo;
import ace.module.product.core.impl.dao.model.bo.PmsProductAttributeSaveBo;
import ace.module.product.core.impl.dao.model.bo.PmsProductSkuSaveBo;
import ace.module.product.core.impl.dao.model.bo.PmsProductSkuSaveListBo;
import ace.module.product.core.impl.manager.PmsCommonGalleryManager;
import ace.module.product.core.impl.manager.PmsProductSkuAttributeValueManager;
import ace.module.product.core.impl.manager.PmsProductSkuManager;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * .
 * @author Caspar
 *
 */
@AllArgsConstructor
@Component
public class PmsProductSkuHelper {

  private final PmsProductSkuManager pmsProductSkuManager;
  private final PmsCommonGalleryManager pmsCommonGalleryManager;
  private final PmsProductSkuAttributeValueManager pmsProductSkuAttributeValueManager;

  public PmsProductSkuSaveListBo assemblePmsProductSkuSaveListBo(
      SaveContext<PmsProduct> pmsProductSaveContext,
      PmsProductAttributeSaveBo pmsProductAttributeSaveBo,
      List<PmsProductSkuSaveInput> pmsProductSkuSaveInputs
  ) {

    PmsProduct pmsProduct = pmsProductSaveContext.getData();
    List<PmsProductSkuSaveBo> productSkuSaveBos = pmsProductSkuSaveInputs
        .stream()
        .map(pmsProductSkuSaveInput -> {
          SaveContext<PmsProductSku> pmsProductSkuSaveContext = pmsProductSkuManager.assembleSaveContext(
              pmsProductSaveContext,
              pmsProductAttributeSaveBo,
              pmsProductSkuSaveInput
          );
          SaveListContext<PmsProductSkuAttributeValue> pmsProductSkuAttributeValueSaveListContext = pmsProductSkuAttributeValueManager.assemblePmsProductSkuAttributeValueSaveListContext(
              pmsProductSkuSaveContext,
              pmsProductAttributeSaveBo.getAttributes(),
              pmsProductAttributeSaveBo.getAttributeValues(),
              pmsProductSkuSaveInput
          );
          SaveContext<PmsCommonGallery> productSkuMainImageSaveContext = pmsCommonGalleryManager.assembleProductSkuMainImageSaveContext(
              pmsProduct.getPmsAppId(),
              pmsProduct.getId(),
              pmsProductSkuSaveContext.getData().getId(),
              pmsProductSkuSaveInput.getMainImgUrl()
          );
          return PmsProductSkuSaveBo.builder()
              .productSku(pmsProductSkuSaveContext)
              .productSkuAttributeValue(pmsProductSkuAttributeValueSaveListContext)
              .productSkuMainImage(productSkuMainImageSaveContext)

              .build();
        })
        .collect(Collectors.toList());

    return PmsProductSkuSaveListBo.builder()
        .productSkuSaveBos(productSkuSaveBos)
        .build();
  }


}
